﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DCS.AdministrationClient.DCS_Service;
using NPlot;

namespace DCS.AdministrationClient.Forms
{
    public partial class MonitoringForm : Form
    {
        private readonly List<int> clientsNumber = new List<int>(50);
        private readonly List<DateTime> timeTicks = new List<DateTime>(50);
        private HardwareDescription summaryCharacteristics;

        private DateTime timeOffset = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));

        public MonitoringForm()
        {
            InitializeComponent();
            timer.Start();
        }

        private void ExitClick(object sender, EventArgs e)
        {
            Program.Greetings.Show();

            Hide();
        }

        private void MonitoringForm_Load(object sender, EventArgs e)
        {
            comboBoxActivityPeriod.SelectedIndex = 1;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                timeTicks.Add(DateTime.Now);
                clientsNumber.Add(Program.Context.DcsService.GetActiveClientsNumber(timeOffset));

                summaryCharacteristics = Program.Context.DcsService.GetSummaryCharacteristics();
                BrowseCharacteristics();
            }
            catch (Exception exc)
            {
            }

            PreparePlot();
            DrawGraph();
        }

        private void PreparePlot()
        {
            // Создание сетки на графике
            var grid = new Grid();

            grid.VerticalGridType = Grid.GridType.Fine;
            grid.HorizontalGridType = Grid.GridType.Fine;
            grid.MajorGridPen = new Pen(Color.LightBlue, 1.0f);

            // Отрисовка сетки
            clientsNumberGraph.Add(grid);
        }

        private void DrawGraph()
        {
            var Graph = new LinePlot();
            Graph.Pen = new Pen(Color.Red, 2.0f);

            Graph.AbscissaData = timeTicks.ToArray();
            Graph.OrdinateData = clientsNumber.ToArray();
            clientsNumberGraph.Add(Graph);
            clientsNumberGraph.Refresh();

            if (timeTicks.Count == 50)
            {
                timeTicks.Clear();
                clientsNumber.Clear();
            }
        }

        private void BrowseCharacteristics()
        {
            textBoxCoresNumber.Text = summaryCharacteristics.CoreCount.ToString();
            textBoxCpuFrequency.Text = summaryCharacteristics.CpuFrequency.ToString();
            textBoxHddFreeSpace.Text = summaryCharacteristics.AvailableMemory.ToString();
            textBoxRAM.Text = summaryCharacteristics.Ram.ToString();
        }

        private void numericIntervalTime_ValueChanged(object sender, EventArgs e)
        {
            timer.Interval = Convert.ToInt32(numericIntervalTime.Value*1000);
        }

        private void comboBoxActivityPeriod_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBoxActivityPeriod.SelectedIndex)
            {
                case 0:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(24*7, 0, 0));
                        break;
                    }
                case 1:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(24, 0, 0));
                        break;
                    }
                case 2:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(1, 0, 0));
                        break;
                    }
                case 3:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 30, 0));
                        break;
                    }
                case 4:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 20, 0));
                        break;
                    }
                case 5:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 10, 0));
                        break;
                    }
                case 6:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 5, 0));
                        break;
                    }
                case 7:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 1, 0));
                        break;
                    }
                case 8:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 0, 30));
                        break;
                    }
                case 9:
                    {
                        timeOffset = DateTime.Now.Subtract(new TimeSpan(0, 0, 15));
                        break;
                    }
            }
        }
    }
}